package com.das.mechanic_base.widget;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.adapter.alone.X3ShareAdapter;
import com.das.mechanic_base.app.X3Constent;
import com.das.mechanic_base.base.X3BaseSelfBottomSheetDialog;
import com.das.mechanic_base.bean.alone.AloneShareBean;
import com.das.mechanic_base.utils.AverageGapItemDecoration;
import com.das.mechanic_base.utils.X3FileUtils;
import com.das.mechanic_base.utils.X3ScreenUtils;
import com.das.mechanic_base.utils.X3ToastUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class X3BottomShareDialog2 extends X3BaseSelfBottomSheetDialog implements X3ShareAdapter.IOnItemClick {

    RecyclerView rlv_share;
    X3ShareAdapter adapter;
    Activity mActivity;
    String picPath;
    String url;
    public X3BottomShareDialog2(@NonNull Context context, Activity mActivity, String picPath) {
        super(context,R.style.dialog2);
        this.mActivity = mActivity;
        this.picPath = picPath;
        Log.e("dP-px",X3ScreenUtils.dipToPx(1,mContext) + "");
    }

    @Override
    protected boolean getDialogCancel() {
        return false;
    }

    @Override
    protected void initView() {
        rlv_share = getView(R.id.rlv_share);
        int count = 0;
        int gapH = 0;
        List<AloneShareBean> dataList = initDataList();
        if (X3Constent.isChina()){
            count = 4;
            gapH = 40;
        } else {
            count = 3;
            gapH = 86;
        }

        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext,count);
        rlv_share.setLayoutManager(gridLayoutManager);
        rlv_share.addItemDecoration(new AverageGapItemDecoration(40,0,0));
        adapter = new X3ShareAdapter(mContext,dataList);
        adapter.setiOnItemClick(this);
        rlv_share.setAdapter(adapter);
    }

    private List<AloneShareBean> initDataList() {
        List<AloneShareBean> dataList = new ArrayList<>();
        if (X3Constent.isChina()){
            AloneShareBean aloneShareBean1 = new AloneShareBean();
            aloneShareBean1.type = AloneShareBean.ShareType.vx;
            aloneShareBean1.des = mContext.getString(R.string.x3_alone_share_vx);
            aloneShareBean1.picId = R.mipmap.x3_alone_share_vx;

            AloneShareBean aloneShareBean2 = new AloneShareBean();
            aloneShareBean2.type = AloneShareBean.ShareType.msg;
            aloneShareBean2.des = mContext.getString(R.string.x3_alone_share_msg);
            aloneShareBean2.picId = R.mipmap.x3_alone_share_msg;

            AloneShareBean aloneShareBean3 = new AloneShareBean();
            aloneShareBean3.type = AloneShareBean.ShareType.url;
            aloneShareBean3.des = mContext.getString(R.string.x3_alone_share_url);
            aloneShareBean3.picId = R.mipmap.x3_alone_share_url;

            AloneShareBean aloneShareBean4 = new AloneShareBean();
            aloneShareBean4.type = AloneShareBean.ShareType.pic;
            aloneShareBean4.des = mContext.getString(R.string.x3_alone_share_pic);
            aloneShareBean4.picId = R.mipmap.x3_alone_share_pic;

            dataList.add(aloneShareBean1);
            dataList.add(aloneShareBean2);
            dataList.add(aloneShareBean3);
            dataList.add(aloneShareBean4);

        } else {

            AloneShareBean aloneShareBean2 = new AloneShareBean();
            aloneShareBean2.type = AloneShareBean.ShareType.msg;
            aloneShareBean2.des = mContext.getString(R.string.x3_alone_share_msg);
            aloneShareBean2.picId = R.mipmap.x3_alone_share_msg;

            AloneShareBean aloneShareBean3 = new AloneShareBean();
            aloneShareBean3.type = AloneShareBean.ShareType.url;
            aloneShareBean3.des = mContext.getString(R.string.x3_alone_share_url);
            aloneShareBean3.picId = R.mipmap.x3_alone_share_url;

            AloneShareBean aloneShareBean4 = new AloneShareBean();
            aloneShareBean4.type = AloneShareBean.ShareType.pic;
            aloneShareBean4.des = mContext.getString(R.string.x3_alone_share_pic);
            aloneShareBean4.picId = R.mipmap.x3_alone_share_pic;

            dataList.add(aloneShareBean2);
            dataList.add(aloneShareBean3);
            dataList.add(aloneShareBean4);
        }

        return dataList;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.x3_alone_share_dialog;
    }

    @Override
    protected int getDialogHeight() {
//        return (int) (X3ScreenUtils.getScreenHeight(mContext) * 0.196);
        return X3ScreenUtils.dipToPx(140,mContext);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        mActivity.finish();
    }

    @Override
    protected boolean getCanceledOnTouchOutside() {
        return false;
    }

    @Override
    protected boolean getControlCanceledOnTouchOutside() {
        return true;
    }

    @Override
    public void iOnItemCameraClick(int index, AloneShareBean.ShareType type) {
        if (type == AloneShareBean.ShareType.vx){

        } else if (type == AloneShareBean.ShareType.msg){

        }
        else if (type == AloneShareBean.ShareType.url){
            copy2Clipboard(url);
        }
        else if (type == AloneShareBean.ShareType.pic){
            Bitmap bitmap = BitmapFactory.decodeFile(picPath);
            File file = new File(picPath);
            X3FileUtils.saveBitmapToCamera(mContext,file.getName() + ".jpg",bitmap);
            X3ToastUtils.showMessage(mContext.getString(R.string.x3_keep_success));
        }
    }

    private void copy2Clipboard(String content) {
        ClipboardManager clipboard = (ClipboardManager) this.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
        //创建ClipData对象
        //第一个参数只是一个标记，随便传入。
        //第二个参数是要复制到剪贴版的内容
        ClipData clip = ClipData.newPlainText("content", content);
        //传入clipData对象.将文本内容放到系统剪贴板里。
        clipboard.setPrimaryClip(clip);
        String tip = "";
        tip = this.getContext().getString(R.string.x3_car_detail_url_copyed);
        X3ToastUtils.showMessage(tip);
    }
}
